package eu.reply.flashmob

import com.google.android.gcm.server.Message
import com.google.android.gcm.server.MulticastResult
import com.google.android.gcm.server.Sender

class PushService {

	private String flash_m_org_key = "AIzaSyCQTlVFbCiQuYZyhsrklh_f_u0v7hUgT0M" 
	
	/**
	 * @param eventInstance a copy of the event in question
	 * @param userProfiles a list of all known profiles
	 **/
    def pushEventToAll(Event eventInstance, List<UserProfile> userProfiles ) {
		System.out.println "enter"
		ArrayList<String> listOfNumbers = new ArrayList<String>();
		for(UserProfile user : userProfiles){
			if(user.getRegistrationId() == null){
				
			}
			else{
				listOfNumbers.add(user.getRegistrationId());
			}
		}
				
				Sender sender = new Sender (flash_m_org_key);
				Message message = new Message.Builder()
				.addData("name", eventInstance.getName())
				.addData("location", eventInstance.getLocation())
				.build();
		//		Message message = new Message.Builder().build();
				int a = 1+1;
			//	String data = "{\"name\":\""+eventInstance.getName()+"\", \"location\":\""+eventInstance.getLocation()+"\"}";
				try{
					MulticastResult result = sender.send(message, listOfNumbers,10);
				}
				catch(Exception e){
					System.out.println(e);
					System.out.println(e.getMessage());
					System.out.println(message.toString());
				}
			
		
		System.out.println "exit"
    }
	
}
